#!/bin/bash
BASE_HOME=`/usr/bin/dirname "$0"`
FILE_NAME=`basename "$0"`
PROCESS_ID="$$"
CONF_FILE="${BASE_HOME}/mysql_config.list"
##load mysql config file mysql_config.list
while read line; do
    eval $line
done < ${CONF_FILE}


#create logfile path if not exists
LOGFILE_PATH=`echo ${LOGFILE%/*}`
if [ ! -d ${LOGFILE_PATH} ]; then
                mkdir -p  ${LOGFILE_PATH} 
fi

##record scripts running logs
exec >> ${LOGFILE} 2>&1

echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived into backup status,if local mysql database slave is not set and seted"

##if this scripts is running and exit
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]check scripts ps -ef | grep \"$0\" | grep -v ${PROCESS_ID} | grep -v grep is running "
check_running=`ps -ef | grep "$0" | grep -v ${PROCESS_ID} | grep -v grep |wc -l`
ps -ef | grep "$0" | grep -v ${PROCESS_ID} | grep -v grep
if [ ${check_running} -ge 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]scripts $0 is running ${check_running} and exit"
                #exit 0
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]scripts $0 is not running ${check_running} and continue to switch"
fi 

##if the write vip in local host and not configure slave 
WVIP_COUNT=`ip addr | grep -v grep | grep "${WRITE_VIP}" |wc -l`
if [ ${WVIP_COUNT} -eq 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP ${WRITE_VIP} on local host ${LOCAL_HOST} mysql node  is master and not need to  configure slave"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]VIP `ip addr | grep -v grep | grep \"${WRITE_VIP}\"`"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end backup status and exit scripts running"
                exit 0
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]VIP ${WRITE_VIP} not exists local host ${LOCAL_HOST} mysql node  is slave and continue configure slave policy"
fi



##check VIP is up  by ping
remote_host_ping_vip()
{
                result_ping=0
                ping -c ${PINGDOWN_C} ${WRITE_VIP} 
                if [ $? -eq 0 ]; then
                                result_ping=1
                else
                    result_ping=0
                fi
}

echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host ${LOCAL_HOST} start ping VIP  \"ping -c ${PINGDOWN_C} ${WRITE_VIP}\" "
i=1
down_time=0
while [ ${i} -le ${PINGDOWN_TOTAL} ]
do
                remote_host_ping_vip
                if [ ${result_ping} -eq 1 ]; then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host ${LOCAL_HOST} ping VIP ${WRITE_VIP} is alived by ${i} times"
                                down_time=1
                else
                    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host ${LOCAL_HOST} ping VIP ${WRITE_VIP} is down by ${i} times"
                    down_time=0
                    break;
                fi
                i=$(($i+1))
done
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]end ping VIP  \"ping -c ${PINGDOWN_C} ${WRITE_VIP}\" "

if [ ${down_time} -eq 1 ]; then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host ${LOCAL_HOST} ping VIP ${WRITE_VIP} is alived and continue set slave policy"
else
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]local host ${LOCAL_HOST} ping VIP ${WRITE_VIP} is down and not need to set slave policy"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end backup status and exit scripts running"
                exit 0
fi 


# mysql CMD
MYSQL_L_CMD="${MYSQL_CMD} -u${MYSQL_USER} -p${MYSQL_PASS} -P${MYSQL_PORT} -h${LOCAL_HOST} --protocol=tcp"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQL_L_CMD is \"${MYSQL_L_CMD}\" "
MYSQL_R_CMD="${MYSQL_CMD} -u${SLAVE_USR} -p${SLAVE_PWD} -P${SLAVE_PORT} -h${REMOTE_HOST} --protocol=tcp"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQL_R_CMD is \"${MYSQL_R_CMD}\" "
MYSQLPING_CMD="${MYSQLADMIN_CMD} -u${SLAVE_USR} -p${SLAVE_PWD} -P${SLAVE_PORT} -h${REMOTE_HOST} ping"
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]MYSQLPING_CMD is \"${MYSQLPING_CMD}\" "
#create binlog parse to sql work dir if not exists
if [ ! -d ${sqlpath} ]; then
                mkdir -p  ${sqlpath} 
fi

##check remote mysql is alive by mysqladmin ping
alive_string=`${MYSQLPING_CMD}`
if [ "${alive_string}"x != "mysqld is alive"x ]; then
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]remote host ${REMOTE_HOST} mysql is not alived and \"${alive_string}\"!"
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end backup status and exit scripts running"
        exit 0
else
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]remote host ${REMOTE_HOST} mysql is alived \"${alive_string}\" and check is or master"
        slave_string=`${MYSQL_R_CMD} -N -L -s -e "show slave status"`
        echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]remote host ${REMOTE_HOST} mysql slave info is \"${slave_string}\""
        if [ "${slave_string}"x != ""x ];then
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]remote host ${REMOTE_HOST} is slave and not need to set slave policy"
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end backup status and exit scripts running"
                exit 0
        else
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]remote host ${REMOTE_HOST} is master role and continue to set slave policy"
        fi
fi




echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]start get local mysql show slave status"
${MYSQL_L_CMD} -e "show slave status \G" > ${sqlpath}/local_slave.txt
if [ $? -eq 0 ]; then
                cat ${sqlpath}/local_slave.txt
                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]get  local mysql show slave status completed"
                slave_host=`cat ${sqlpath}/local_slave.txt |grep -w "Master_Host"|awk -F": " '{print $2}'`
                if [ "${slave_host}"x != ""x ]; then
                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host slave configured Master_Host=${slave_host}"
                else
                    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[warn]local host slave not configured Master_Host=${slave_host}"
                    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host slave set start set global super_read_only=OFF;CHANGE MASTER TO MASTER_HOST = '${REMOTE_HOST}', MASTER_USER = '${REP_USER}', MASTER_PASSWORD = '${REP_PWD}',MASTER_PORT=${REP_PORT},MASTER_AUTO_POSITION = 1;"
                                ${MYSQL_L_CMD} -N -L -s -e "set  global super_read_only=OFF;set global read_only=ON;show variables like '%read_only';"
                                ${MYSQL_L_CMD} -e "CHANGE MASTER TO MASTER_HOST = '${REMOTE_HOST}', MASTER_USER = '${REP_USER}', MASTER_PASSWORD = '${REP_PWD}',MASTER_PORT=${REP_PORT},MASTER_AUTO_POSITION = 1;reset slave;start slave;"
                                if [ $? -eq 0 ]; then
                                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host slave set completed"
                                                ${MYSQL_L_CMD} -e "set global super_read_only=ON;show variables like 'super_read_only';show variables like 'read_only';show slave status \G"

                                else
                                                echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]local host slave set failed"
                                fi

          fi
          rm -f ${sqlpath}/local_slave.txt
else
    echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]get local mysql show slave status failed"
fi
echo "`date +'%F %T'`|${FILE_NAME}|${PROCESS_ID}|[info]keepalived end backup status and exit scripts running"
